package com.example.demo.service.Impl;

import com.example.demo.common.domain.Record;
import com.example.demo.common.domain.RecordExample;
import com.example.demo.mapper.RecordMapper;
import com.example.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service("recordServiceImpl")
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public List<Record> selectByEid(int eid) {
        RecordExample example = new RecordExample();
        example.createCriteria().andEidEqualTo(eid);
        return recordMapper.selectByExample(example);
    }

    @Override
    public Record selectByEidAndDate(int eid, Date date) {
        RecordExample example = new RecordExample();
        example.createCriteria().andEidEqualTo(eid).andRdateEqualTo(date);
        List<Record> records = recordMapper.selectByExample(example);
        return records.size() == 0?null: records.get(0);
    }

    @Override
    public void insertSelective(Record record) {
        recordMapper.insertSelective(record);
    }

    @Override
    public void updateXBTime(int eid,Date now) {
        Record r = selectByEidAndDate(eid,now);
        r.setRendtime(now);
        recordMapper.updateByPrimaryKeySelective(r);
    }

    @Override
    public List<Record> selectByEidDateBetween(int eid, int from, int to) {
        RecordExample example = new RecordExample();
        Date now = new Date();

        Date fromD = new Date(now.getYear(),now.getMonth(),from);
        Date toD = new Date(now.getYear(),now.getMonth(),to);
        example.createCriteria().andEidEqualTo(eid).andRdateBetween(fromD,toD);
        return recordMapper.selectByExample(example);
    }

    @Override
    public Record selectByEidDateEq(int eid, int day) {
        RecordExample example = new RecordExample();
        Date now = new Date();

        Date date = new Date(now.getYear(),now.getMonth(),day);
        example.createCriteria().andEidEqualTo(eid).andRdateEqualTo(date);
        List<Record> records = recordMapper.selectByExample(example);
        return records.size() == 0?null: records.get(0);
    }
}
